package cn.gson.crm.controller.student;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.ClassDao;
import cn.gson.crm.model.dao.CustomDao;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.entity.ClassEntity;
import cn.gson.crm.model.entity.Custom;
import cn.gson.crm.model.entity.Student;
import cn.gson.crm.vo.CustomVo;

/**
 * 分配用户到班级
 */
@WebServlet("/student/assign")
public class StudentAssignServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private CustomDao customDao = new CustomDao();
	private StudentDao studentDao = new StudentDao();
	private ClassDao classDao = new ClassDao();
	String password="111111";

	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String deptid = request.getParameter("deptid");
		String[] ids = request.getParameterValues("id");
		String stupassword=addPass(password);
		for (String id : ids) {
			// 获得选中的id的客户信息
			Custom custom = customDao.getOneCustomByid(id);

			// 获得id对应的学生信息
			Student student = studentDao.getOneStudentById(id);
			if (student != null && custom.getCustomId().equals(student.getStuId())&&custom.getCustomName().equals(student.getStuName())) {
				requesetAll(request);
				errorForward("系统出错,请联系维护人员(数据出问题)", "student/assignstudent.jsp");
				return;
			}
			boolean resultinsert = studentDao.insertOneStudent(custom, deptid,stupassword);
			if (!resultinsert) {
				requesetAll(request);
				errorForward("分配失败", "student/assignstudent.jsp");
				return;
			}
			boolean resultdelete = customDao.daleteCustom(id);
			if (!resultdelete) {
				requesetAll(request);
				errorForward("处理失败", "student/assignstudent.jsp");
				return;
			}
		}
		redirect("student/main");
	}

	private void requesetAll(HttpServletRequest request) throws SQLException {
		List<ClassEntity> classList = classDao.getClassAll();
		String p = request.getParameter("page");
		if (p == null || p.isEmpty()) {
			p = "1";
		}
		Integer page = Integer.parseInt(p);
		// 获得全部信息
		List<CustomVo> customVoList = customDao.getAllCustomByStatus(3, page);
		// 总页数
		int totalPage = customDao.getTotalPage(3);
		request.setAttribute("queryString", noPageQueryString(request));
		request.setAttribute("customList", customVoList);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("page", page);
		request.setAttribute("classList", classList);
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	private String noPageQueryString(HttpServletRequest request) {
		Map<String, String[]> params = request.getParameterMap();

		String paramsStr = "";
		int i = 0;
		for (String key : params.keySet()) {
			if ("page".equals(key)) {
				continue;
			}
			String[] valus = params.get(key);
			String query = "";
			for (int j = 0; j < valus.length; j++) {
				query += key + "=" + valus[j];
				if (j < valus.length - 1) {
					query += "&";
				}
			}
			paramsStr += query;
			if (i < params.keySet().size() - 1) {
				paramsStr += "&";
			}
			i++;
		}
		return paramsStr.isEmpty() ? "" : "&" + paramsStr;
	}

}
